<template>
    <new-search-panel :showAdvanced="false" @search="onSearch" @reset="onReset">
        <template #default>
            <a-col :md="24" :sm="24">
                <a-form-item label="应用名称">
                    <a-input v-model:value="queryParam.name" :maxLength="35" allow-clear placeholder="请输入应用名称" />
                </a-form-item>
            </a-col>
            <a-col :md="24" :sm="24">
                <a-form-item label="唯一编码">
                    <a-input v-model:value="queryParam.code" :maxLength="35" allow-clear placeholder="请输入唯一编码" />
                </a-form-item>
            </a-col>
        </template>
        <template #button>
            <a-button type="primary" @click="addForm()" v-if="$hasPerm('sysUser:add')">
                <template #icon><PlusOutlined /></template>
                新增应用</a-button
            >
        </template>
    </new-search-panel>

    <dynamic-table
        :scrollX="1000"
        :fixed="true"
        ref="tableRef"
        :tableType="1"
        :columns="columns"
        :get-list-func="getAppPage"
        :queryParam="queryParam"
        rowKey="id"
    >
        <template #active="{ text }">
            {{ filterEnum(activeDict, text) }}
        </template>

        <template #status="{ text }">
            {{ filterEnum(statusDict, text) }}
        </template>

        <template #action="{ record }">
            <a @click="editForm(record)" v-if="$hasPerm('sysUser:edit')">编辑</a>
            <a-divider type="vertical" />
            <a-popconfirm placement="topRight" title="确认删除？" @confirm="appDelete(record)">
                <a>删除</a>
            </a-popconfirm>
            <a-divider type="vertical" />
            <a-popconfirm
                v-if="$hasPerm('sysApp:setAsDefault') & (record.active == 'N')"
                placement="topRight"
                title="设置为默认应用？"
                @confirm="() => sysDefault(record)"
            >
                <a>设为默认</a>
            </a-popconfirm>
        </template>
    </dynamic-table>
</template>
<script lang="ts">
import { defineComponent, reactive, toRefs, onMounted, ref } from 'vue'
import { message, Modal } from 'ant-design-vue'
import { newSearchPanel, DynamicTable } from '@/components'
import { columns } from './columns'
import { filterEnum } from '@/utils/common'
import { getAppPage, getAppList, sysAppDelete, sysAppEdit, sysAppAdd, sysAppSetAsDefault } from '@/api/system/app'
import { useStore } from '@/store'
import { getFormSchema } from './form-schema'
import { useFormModal } from '@/hooks/useFormModal'

export default defineComponent({
    name: 'sys_app_mgr',
    components: {
        newSearchPanel,
        DynamicTable
    },
    setup() {
        const tableRef = ref<any>(null)
        const formRef = ref<any>(null)
        const store = useStore()
        const state = reactive({
            queryParam: {
                name: '',
                code: null
            },
            id: '',
            title: '',
            visible: false,
            activeDict: [],
            statusDict: [],
            tableLoading: false
        })
        onMounted(() => {
            store.dispatch('enums/getEnum', 'yes_or_no').then((res) => {
                state.activeDict = res
            })
            store.dispatch('enums/getEnum', 'common_status').then((res) => {
                state.statusDict = res
            })
        })
        /**
         * 新增应用
         */
        const addForm = () => {
            useFormModal({
                title: '新增应用',
                formSchema: getFormSchema(),
                handleOk: async (modelRef, state) => {
                    await sysAppAdd(modelRef)
                    tableRef.value.refreshTableData()
                }
            })
        }
        /**
         * 编辑应用
         */
        const editForm = (record) => {
            useFormModal({
                title: '编辑应用',
                formSchema: getFormSchema(),
                fields: record,
                handleOk: async (modelRef, state) => {
                    delete modelRef.dataScopeType
                    await sysAppEdit(modelRef)
                    tableRef.value.refreshTableData()
                }
            })
        }

        /**
         * 删除应用
         * @param record
         */
        function appDelete(record) {
            sysAppDelete(record).then(() => {
                message.success('删除成功')
                tableRef.value.refreshTableData()
            })
        }
        /**
         * 设为默认应用
         * @param record
         */
        function sysDefault(record) {
            sysAppSetAsDefault(record).then(() => {
                message.success('设置成功')
                tableRef.value.refreshTableData()
            })
        }

        /**
         * 查询
         */
        function onSearch() {
            tableRef.value.queryParam = state.queryParam
            tableRef.value.resetPagination()
            tableRef.value.refreshTableData()
        }

        /**
         * 重置
         */
        function onReset() {
            state.queryParam = {
                name: '',
                code: null
            }
            // 重置分页
            onSearch()
        }

        return {
            ...toRefs(state),
            columns,
            tableRef,
            getAppList,
            getAppPage,
            onReset,
            onSearch,
            addForm,
            editForm,
            formRef,
            appDelete,
            filterEnum,
            sysDefault
        }
    }
})
</script>

